package com.example.controller;

import com.example.utils.Result;
import com.example.entity.Hengxiang;
import com.example.service.HengxiangService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/hengxiang")
public class HengxiangController {

    @Resource
    HengxiangService hengxiangService;

    // 添加横向项目
    @PostMapping("/add")
    public Result add (@RequestBody Hengxiang hengxiang, HttpSession session){
        hengxiang.setId((Integer) session.getAttribute("id"));
        String msg = hengxiangService.add(hengxiang);
        if (msg != null) {
            return Result.error(msg);
        }
        return Result.success();
    }

    // 删除横向项目
    @DeleteMapping("/delete/{id}")
    public Result delete (@PathVariable Integer id){
        hengxiangService.deleteById(id);
        return Result.success();
    }

    // 查看横向项目详情
    @GetMapping("/detail/{id}")
    public Result detail (@PathVariable Integer id){
        return Result.success(hengxiangService.getById(id));
    }

    // 更新横向项目信息
    @PutMapping("/update")
    public Result update (@RequestBody Hengxiang hengxiang) {
        hengxiangService.updateById(hengxiang);
        return Result.success();
    }

    // 分页查询横向项目
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Hengxiang hengxiang,
                             HttpSession session){
        hengxiang.setId((Integer) session.getAttribute("id"));
        PageInfo<Hengxiang> PageInfo = hengxiangService.selectPage(pageNum, pageSize, hengxiang);
        return Result.success(PageInfo);
    }
}
